#!/bin/bash

set -e

source "$(dirname $0)/android-sdk"

ANDROID_CMDTOOLS=$(find_android_cmdline_tools)

# Dokka stuff
ROOT="$PWD"
DOKKA_CLI_JAR=https://repo1.maven.org/maven2/org/jetbrains/dokka/dokka-cli/1.7.20/dokka-cli-1.7.20.jar

declare -a DOKKA_DEPS=(
https://repo1.maven.org/maven2/org/jetbrains/dokka/dokka-base/1.7.20/dokka-base-1.7.20.jar
https://repo1.maven.org/maven2/org/jetbrains/dokka/dokka-analysis/1.7.20/dokka-analysis-1.7.20.jar
https://repo1.maven.org/maven2/org/jetbrains/dokka/kotlin-analysis-intellij/1.7.20/kotlin-analysis-intellij-1.7.20.jar
https://repo1.maven.org/maven2/org/jetbrains/dokka/kotlin-analysis-compiler/1.7.20/kotlin-analysis-compiler-1.7.20.jar
https://repo1.maven.org/maven2/org/jetbrains/kotlinx/kotlinx-coroutines-core/1.6.4/kotlinx-coroutines-core-1.6.4.jar
https://repo1.maven.org/maven2/org/jetbrains/kotlinx/kotlinx-html-jvm/0.8.0/kotlinx-html-jvm-0.8.0.jar
https://repo1.maven.org/maven2/org/freemarker/freemarker/2.3.31/freemarker-2.3.31.jar
)

$ANDROID_CMDTOOLS/sdkmanager --verbose "cmake;3.18.1" "ndk;23.1.7779620"
$ANDROID_CMDTOOLS/sdkmanager "system-images;android-30;google_apis;x86"

echo -e "y\ny\ny\ny\ny\n" | $ANDROID_CMDTOOLS/sdkmanager --licenses

echo "Downloading Dokka..."
DOKKA_DIR="$ROOT/build/dokka"
mkdir -p "$DOKKA_DIR"
cd "$DOKKA_DIR"

if [ ! -f dokka-cli-1.7.20.jar ]; then
    curl -fSsOL $DOKKA_CLI_JAR
fi

for dep in "${DOKKA_DEPS[@]}" ; do
    if [ ! -f ${dep##*/} ]; then
        curl -fSsOL $dep
    fi
done
